package com.springcloud.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import jakarta.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.Map;

/**
 * Spring Boot 3.x + JDK 17 集成验证应用
 * 
 * 用于验证 Spring Boot 升级后的核心功能：
 * - Spring Boot 3.x 启动正常
 * - Jakarta Servlet API 工作正常  
 * - Web 控制器响应正常
 * - 依赖注入工作正常
 * 
 * @author SpringCloud团队
 * @version 1.0.0
 */
@SpringBootApplication
@RestController
public class SpringBootVerificationApp {

    public static void main(String[] args) {
        System.out.println("==========================================");
        System.out.println("  Spring Boot 3.x + JDK 17 验证启动中...");
        System.out.println("==========================================");
        
        SpringApplication.run(SpringBootVerificationApp.class, args);
        
        System.out.println("==========================================");
        System.out.println("  ✅ Spring Boot 应用启动成功！");
        System.out.println("  🌐 访问 http://localhost:8888/verify");
        System.out.println("==========================================");
    }

    /**
     * 第一步：基础健康检查接口
     */
    @GetMapping("/verify")
    public Map<String, Object> verifyUpgrade(HttpServletRequest request) {
        return Map.of(
            "status", "success",
            "message", "🎉 Spring Boot 3.x + JDK 17 升级验证成功！",
            "timestamp", LocalDateTime.now(),
            "javaVersion", System.getProperty("java.version"),
            "springBootVersion", getClass().getPackage().getImplementationVersion(),
            "servletApi", "jakarta.servlet.http.HttpServletRequest",
            "requestPath", request.getRequestURI(),
            "features", Map.of(
                "textBlocks", "支持",
                "switchExpressions", "支持", 
                "patternMatching", "支持",
                "jakartaEE", "已迁移"
            )
        );
    }

    /**
     * 第二步：JDK 17 特性展示接口
     */
    @GetMapping("/features")
    public Map<String, Object> showJdk17Features() {
        // 使用文本块
        String welcomeMessage = """
                欢迎使用 JDK 17 + Spring Boot 3.x！
                
                主要特性：
                ✅ 文本块支持
                ✅ Switch 表达式
                ✅ 模式匹配
                ✅ Jakarta EE 规范
                ✅ 性能优化
                """;

        // 使用 Switch 表达式
        String seasonMessage = switch (LocalDateTime.now().getMonthValue()) {
            case 3, 4, 5 -> "春暖花开的季节";
            case 6, 7, 8 -> "炎炎夏日";
            case 9, 10, 11 -> "秋高气爽";
            case 12, 1, 2 -> "寒冷冬季";
            default -> "未知季节";
        };

        return Map.of(
            "welcomeMessage", welcomeMessage.strip(),
            "currentSeason", seasonMessage,
            "timestamp", LocalDateTime.now(),
            "jdk17Features", Map.of(
                "textBlocks", true,
                "switchExpressions", true,
                "patternMatching", true,
                "performance", "enhanced"
            )
        );
    }

    /**
     * 第三步：Jakarta Servlet API 验证接口
     */
    @GetMapping("/servlet-api")
    public Map<String, Object> verifyServletApi(HttpServletRequest request) {
        return Map.of(
            "servletApiVersion", "Jakarta Servlet API",
            "requestMethod", request.getMethod(),
            "requestURI", request.getRequestURI(),
            "serverInfo", request.getServletContext().getServerInfo(),
            "characterEncoding", request.getCharacterEncoding(),
            "contentType", request.getContentType(),
            "verification", "✅ Jakarta Servlet API 工作正常"
        );
    }

    /**
     * 第四步：错误处理验证
     */
    @GetMapping("/error-test")
    public Map<String, Object> errorHandling() {
        try {
            // 模拟一个可能的错误
            String test = null;
            return Map.of("length", test.length());
        } catch (Exception e) {
            return Map.of(
                "error", "预期的异常处理测试",
                "message", e.getMessage(),
                "type", e.getClass().getSimpleName(),
                "verification", "✅ 异常处理机制正常"
            );
        }
    }
}